package com.lingtianyu.tech.erbaoshen.core.provider.bean.registry;

import com.lingtianyu.tech.erbaoshen.core.provider.bean.annotation.ErBaoShenService;
import com.lingtianyu.tech.erbaoshen.core.provider.bean.annotation.Service;
import com.lingtianyu.tech.erbaoshen.core.util.ErBaoShenFinals;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;

public class ScannerImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware {
    private Environment environment;

    @Override
    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
        scanner.addIncludeFilter(new AnnotationTypeFilter(ErBaoShenService.class));
        scanner.addIncludeFilter(new AnnotationTypeFilter(Service.class));
        //从指定包路径下扫描
        String property = environment.getProperty(ErBaoShenFinals.PROPERTIES_SCAN);
        if(property == null && property.isEmpty()){
            throw new RuntimeException(ErBaoShenFinals.PROPERTIES_SCAN + " must be not null");
        }
        String[] split = property.split(",");
        scanner.scan(split);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}